package classconverstation.cscl.ist.psu.edu.client.messengers;

import java.io.Serializable;
import java.util.Date;

import classconverstation.cscl.ist.psu.edu.client.CommonObject;
import classconverstation.cscl.ist.psu.edu.client.services.ClassCommonsService;
import classconverstation.cscl.ist.psu.edu.client.services.ClassCommonsServiceAsync;
import classconverstation.cscl.ist.psu.edu.client.util.ClassConversationUtil;


import com.google.gwt.core.client.GWT;

import com.google.code.p.gwtchismes.client.GWTCPopupBox;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;

import com.google.gwt.user.client.rpc.AsyncCallback;

import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.FileUpload;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;

import com.google.gwt.user.client.ui.Frame;

import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.HorizontalPanel;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/*
 * @Honglu Du
 * Object that stores a ClassCommons message
 * 
 */
public class ClassCommonsMessage implements Serializable {

	int cid;
	int mid;
	User poster;
	String uid;
	String content;
	String videoURL;
	String imageURL;
	Date postDate;
	boolean isTopic;
	int like;
	boolean isChecked;
	int type;
	//CheckBox ck;
	
	
	
	public ClassCommonsMessage(){

	}

	public ClassCommonsMessage(User u, int cid, String c, String vl, String il, Date pd, boolean it, int likes, int type){
		this.poster = u;
		this.cid = cid;
		this.content = c;
		this.videoURL = vl;
		this.imageURL = il;
		this.postDate = pd;
		this.isTopic = it;
		this.like = likes;
		isChecked = false;
		this.type = type;
	}

	public ClassCommonsMessage(int mid, int cid, User u,  String c, String vl, String il, Date pd, boolean it, int likes, int type){
		this.mid = mid;
		this.poster = u;
		this.cid = cid;
		this.content = c;
		this.videoURL = vl;
		this.imageURL = il;
		this.postDate = pd;
		this.isTopic = it;
		this.like = likes;
		isChecked = false;
		this.type = type;
		
	}

	public int getMid(){
		return this.mid;
	}
	
	public int getCid(){
		return this.cid;
	}
	
	public void setCid(int c_id){
		this.cid = c_id;
	}

	public User getPoster(){
		return this.poster;
	}

	public String getContent(){
		return this.content;
	}

	public String getVideoURL(){
		return this.videoURL;
	}

	public String getImageURL(){
		return this.imageURL;
	}

	public Date getPostDate(){
		return this.postDate;
	}

	public boolean isTopic(){
		return this.isTopic;
	}
	public void setIsTopic(boolean t){
		this.isTopic = t;
	}

	public int getLikes(){
		return this.like;
	}

	public void setLikes(int likes){
		this.like = likes;
	}
	
	public void setType(int type){
		this.type = type;
	}
	
	public int getType(){
		return this.type;
	}
	
	public FlowPanel getPinBox(int width){
		Image pin = new Image("../images/pin.png");
		FlowPanel fPanel = new FlowPanel();
	

		fPanel.addStyleName("clear");
		//fPanel.addStyleName("pin-backgroundcolor");
		FlowPanel messagePanel = new FlowPanel();
		FlexTable fTable = new FlexTable();
		String name = poster.getFName();
		Label posterLabel = new Label(name+": ");
		posterLabel.addStyleDependentName("nameColor");
		fTable.setWidget(0, 0, pin);
		fTable.setWidget(0, 1, posterLabel);
		fTable.setText(0, 2, this.content);
		
		fTable.getCellFormatter().addStyleName(0, 1, "nameField");
		fTable.getCellFormatter().addStyleName(0, 2, "nameField");
		
		messagePanel.add(fTable);

		if (!ClassConversationUtil.IsNullOrEmpty(videoURL) || !ClassConversationUtil.IsNullOrEmpty(imageURL)){
			FlexTable mediaTable = getMediaTable();
			messagePanel.add(mediaTable);
		}

		FlowPanel metaDataPanel = new FlowPanel();
		//metaDataPanel.addStyleName("relative");
		FlowPanel  leftDatePanel= new FlowPanel();
		String dateTime = DateTimeFormat.getFormat("yyyy-MM-dd h:mm a").format(postDate);
		Label dateLabel = new Label(dateTime);
		//dateLabel.addStyleDependentName("leftAlign");
		leftDatePanel.add(dateLabel);

		FlowPanel rightOptionPanel = new FlowPanel();
		rightOptionPanel.addStyleName("floatRight");
		
		final Label likeLabel = new Label("Like("+this.like+")");
		likeLabel.addClickHandler(new ClickHandler(){
			
			public void onClick(ClickEvent event) {
				likeLabel.setText("Like("+ ++like +")");
				updateLikeCounts();
			}

		});

		Label bar = new Label("|");
		FlexTable mTable = new FlexTable();

		
		likeLabel.addStyleDependentName("linkleft");
		bar.addStyleDependentName("bar");

		rightOptionPanel.add(bar);
		bar.removeFromParent();
		rightOptionPanel.add(likeLabel);

		mTable.setWidget(0, 0, leftDatePanel);
		mTable.setWidget(0, 1, rightOptionPanel);
		
		mTable.setWidth(width +"px");
		metaDataPanel.add(mTable);
		fPanel.add(messagePanel);
		fPanel.add(metaDataPanel);

		
		return fPanel;
		
	}

	public FlowPanel getMessageBox(int width){
		FlowPanel fPanel = new FlowPanel();
		fPanel.addStyleName("clear");
		FlowPanel messagePanel = new FlowPanel();
		FlexTable fTable = new FlexTable();
		String name = poster.getFName();
		Label posterLabel = new Label(name+": ");
		posterLabel.addStyleDependentName("nameColor");
		fTable.setWidget(0, 0, posterLabel);
		fTable.getCellFormatter().addStyleName(0, 0, "nameField");
		fTable.setText(0, 1, this.content);
		messagePanel.add(fTable);

		if (!ClassConversationUtil.IsNullOrEmpty(videoURL) || !ClassConversationUtil.IsNullOrEmpty(imageURL)){
			FlexTable mediaTable = getMediaTable();
			messagePanel.add(mediaTable);
		}

		FlowPanel metaDataPanel = new FlowPanel();
		//metaDataPanel.addStyleName("relative");
		FlowPanel  leftDatePanel= new FlowPanel();
		String dateTime = DateTimeFormat.getFormat("yyyy-MM-dd h:mm a").format(postDate);
		Label dateLabel = new Label(dateTime);
		//dateLabel.addStyleDependentName("leftAlign");
		leftDatePanel.add(dateLabel);

		FlowPanel rightOptionPanel = new FlowPanel();
		rightOptionPanel.addStyleName("floatRight");
		final Label topic;
		if (!isTopic){
			topic = new Label("Make it a Topic");
			
			topic.addClickHandler(new ClickHandler(){
				
				public void onClick(ClickEvent event) {
//				    Window.alert(Window.Location.getHref());
	                final GWTCPopupBox upload_box = new GWTCPopupBox(GWTCPopupBox.OPTION_ROUNDED_BLUE);
	                ListBox forum_list = new ListBox(false);
	                
	                
	                final FormPanel uploader_form_panel = new FormPanel();
	                uploader_form_panel.setAction("http://cscl.ist.psu.edu:5678/classconversation/service/ForumService"); //need to change to the actual url that handles uploading
	                uploader_form_panel.setEncoding(FormPanel.ENCODING_MULTIPART);
	                uploader_form_panel.setMethod(FormPanel.METHOD_POST);
	                
	                CaptionPanel upload_caption_panel = new CaptionPanel("Make it a discussion topic in the class forum");
	                
	                final VerticalPanel upload_form_element_panel = new VerticalPanel();
	                upload_caption_panel.add(upload_form_element_panel);
	                uploader_form_panel.setWidget(upload_caption_panel);
	                
	                HorizontalPanel hpanel_0 = new HorizontalPanel();
	                Label forum_list_label = new Label("Add it to: ");
	                hpanel_0.add(forum_list_label);
	                
	                
	                
	                prepareDropdownList(Integer.toString(cid), forum_list);
	                forum_list.addChangeHandler(new ChangeHandler() {
	                    public void onChange(ChangeEvent event) {
	                        ListBox list = (ListBox) event.getSource();
	                        Window.alert(list.getValue(list.getSelectedIndex()));
	                    }
	                });
	                forum_list.setName("parent_id");
	                hpanel_0.add(forum_list);
	                
	                upload_form_element_panel.add(hpanel_0);

	                
	                HorizontalPanel hpanel_1 = new HorizontalPanel();
	                  Label title_label = new Label("Title: ");
	                  hpanel_1.add(title_label);
	                  TextBox title = new TextBox();
	                  title.setText("Untitled");
	                  title.setName("title");
	                  
	                  title.addFocusHandler(new FocusHandler() {
	                    public void onFocus(FocusEvent event) {
	                        TextBox box = (TextBox)event.getSource();
	                        String tmp_str = box.getText();
	                        if (tmp_str.equalsIgnoreCase("Untitled")) {
	                            box.setText("");
	                        }
	                        else {
	                            return;
	                        }
	                    }
	                  });
	                  
	                  title.addBlurHandler(new BlurHandler() {
	                    public void onBlur(BlurEvent event) {
	                        TextBox box = (TextBox)event.getSource();
	                        String tmp_str = box.getText();
	                        if (tmp_str.equalsIgnoreCase("Untitled") || tmp_str.trim().equalsIgnoreCase("")) {
	                            box.setText("Untitled");
	                        }
	                        else {
	                            return;
	                        }
	                    }
	                  });
	                  
	                  hpanel_1.add(title);
	                  upload_form_element_panel.add(hpanel_1);
	                
	                HorizontalPanel hpanel_2 = new HorizontalPanel();
	                Label content_label = new Label("Content: ");
	                hpanel_2.add(content_label);
	                TextArea content_box = new TextArea();
	                content_box.setSize("300px", "100px");
	                content_box.setName("content");
	                content_box.setText(content);
	                hpanel_2.add(content_box);
	                upload_form_element_panel.add(hpanel_2);
	                
	                content_box.addFocusHandler(new FocusHandler() {
	                  public void onFocus(FocusEvent event) {
	                      TextArea box = (TextArea)event.getSource();
	                      String tmp_str = box.getText();
	                      if (tmp_str.equalsIgnoreCase("Enter content here.")) {
	                          box.setText("");
	                      }
	                      else {
	                          return;
	                      }
	                  }
	                });
	                  
	                content_box.addBlurHandler(new BlurHandler() {
	                  public void onBlur(BlurEvent event) {
	                      TextArea box = (TextArea)event.getSource();
	                          String tmp_str = box.getText();
	                          if (tmp_str.equalsIgnoreCase("Enter content here.") || tmp_str.trim().equalsIgnoreCase("")) {
	                              box.setText("Enter content here.");
	                          }
	                          else {
	                              return;
	                          }
	                  }
	                });
	                
	                
	                Hidden operator = new Hidden();
	                operator.setName("op");
	                operator.setValue("addAttachment");
	                upload_form_element_panel.add(operator);
	  
	                Hidden redirector = new Hidden();
	                redirector.setName("redirect");
	                redirector.setValue("null");
	                upload_form_element_panel.add(redirector);
	                
	                Hidden hidden_itype_name = new Hidden();
	                hidden_itype_name.setName("itype");
	                hidden_itype_name.setValue("0");
	                upload_form_element_panel.add(hidden_itype_name);
	                
	                Hidden hidden_user_name = new Hidden();
	                hidden_user_name.setName("uid");
	                hidden_user_name.setValue(poster.getUid());
	                upload_form_element_panel.add(hidden_user_name);
	                
	                Hidden hidden_class_id = new Hidden();
	                hidden_class_id.setName("cls");
	                hidden_class_id.setValue(Integer.toString(cid));
	                upload_form_element_panel.add(hidden_class_id);
	  
//	                Hidden hidden_parent_id = new Hidden();
//	                hidden_parent_id.setName("parent_id");
//	                hidden_parent_id.setValue(forum_id);
//	                upload_form_element_panel.add(hidden_parent_id);
	  
	                Hidden hidden_object_type = new Hidden();
	                hidden_object_type.setName("type");
	                hidden_object_type.setValue("1");
	                upload_form_element_panel.add(hidden_object_type);
	                
	                
	                
	                final FileUpload upload = new FileUpload() {
	                    {
	                        addDomHandler(new ChangeHandler() {
	  
	                            
	                            public void onChange(
	                                    ChangeEvent event) {
	                                FileUpload upload = (FileUpload)event.getSource();
	                                VerticalPanel upload_form_element_panel = (VerticalPanel) upload.getParent();
	                                int upload_idx = upload_form_element_panel.getWidgetIndex(upload);
	                                VerticalPanel file_list = (VerticalPanel) upload_form_element_panel.getWidget(upload_idx - 1);
	                                file_list.clear();
	                                String[] names = getFiles(((FileUpload)event.getSource()).getElement());
	                                for (int i = 0; i < names.length; i++) {
	                                    file_list.add(new Label(names[i]));
	                                }
	                            }
	                        }, ChangeEvent.getType());
	                    }
	                };
	                upload.setName("fname");
	                upload.getElement().setAttribute("multiple", "");
	                upload_form_element_panel.add(new VerticalPanel());
	                upload_form_element_panel.add(upload);
	                
	                
	             // Add a 'submit' button.
	                HTML submit_button = new HTML("<div>Submit<span/><div>");
	                submit_button.setStyleName("round-button");
	                submit_button.getElement().setId("round-button-g");
	                submit_button.addClickHandler(new ClickHandler() {
	                  public void onClick(ClickEvent event) {
	                      String[] names = getFiles(upload.getElement());
	                      if (names.length < 1) {
	                          Window.alert("No file is selected!");
	                          return;
	                      }
	                      for (int i = 0; i < names.length; i++) {
	                          if (!names[i].substring(names[i].lastIndexOf('.') + 1).equalsIgnoreCase("pdf")) {
//	                              Window.alert("can't upload files not in pdf format");
//	                              return;
	                          }
	                      }
	                      uploader_form_panel.submit();
	                  }
	                });
	                
	                HTML cancel_button = new HTML("<div>Cancel<span/><div>");
	                cancel_button.setStyleName("round-button");
	                cancel_button.getElement().setId("round-button-g");
	                cancel_button.addClickHandler(new ClickHandler() {
	                  public void onClick(ClickEvent event) {
	                      upload_box.hide();
	                  }
	                });
	                
	                HorizontalPanel submit_cancel_button_panel = new HorizontalPanel();
	                submit_cancel_button_panel.add(submit_button);
	                submit_cancel_button_panel.add(cancel_button);
	                upload_form_element_panel.add(submit_cancel_button_panel);
	  
	                // Add an event handler to the form.
	                uploader_form_panel.addSubmitHandler(new FormPanel.SubmitHandler() {
	                  public void onSubmit(SubmitEvent event) {
	                      String[] names = getFiles(upload.getElement());
	                      if (names.length < 1) {
	                          Window.alert("No file is selected!");
	                      }
	                  }
	                });
	                uploader_form_panel.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
	                  public void onSubmitComplete(SubmitCompleteEvent event) {
	                      upload_box.hide();
	                  }
	                });
	                

	                upload_box.add(uploader_form_panel);

	                int X_ = calculateXposition(event.getNativeEvent().getClientX(), Window.getClientWidth(), 200);
	                int Y_ = calculateYposition(event.getNativeEvent().getClientY(), Window.getClientHeight(), 100);
	                if (event.getNativeEvent().getClientY() + 200 > Window.getClientHeight()) {
	                    upload_box.setPopupPosition(X_, Y_);
	                }
	                else {
	                    upload_box.setPopupPosition(X_, Y_);
	                }
	                upload_box.setZIndex(9999);
	                upload_box.show();
				}
			});
		}
		else{
			topic = new Label ("Discuss It in Forum");
			topic.addClickHandler(new ClickHandler(){
				
				public void onClick(ClickEvent event) {
					Window.alert("Going to the forum to discuss it!");
				}

			});
		}
		
	
		final Label likeLabel = new Label("Like("+this.like+")");
		likeLabel.addClickHandler(new ClickHandler(){
			
			public void onClick(ClickEvent event) {
				likeLabel.setText("Like("+ ++like +")");
				updateLikeCounts();
			}

		});

		Label bar = new Label("|");
		FlexTable mTable = new FlexTable();

		topic.addStyleDependentName("linkleft");
		likeLabel.addStyleDependentName("linkleft");
		bar.addStyleDependentName("bar");
		//rightOptionPanel.add(topic);
		//rightOptionPanel.add(bar);
		rightOptionPanel.add(likeLabel);

		mTable.setWidget(0, 0, leftDatePanel);
		mTable.setWidget(0, 1, rightOptionPanel);
		//int width = (int) (0.6*Window.getClientWidth());
		mTable.setWidth(width+"px");
		metaDataPanel.add(mTable);
		fPanel.add(messagePanel);
		fPanel.add(metaDataPanel);
		
		
		
		return fPanel;
	}
	
	public FlowPanel getMessageBoxCheckable(int width){
		FlowPanel holdingPanel = new FlowPanel();
		FlowPanel fPanel = new FlowPanel();
		fPanel.addStyleName("clear");
		/*
		if (type == 3){
			fPanel.addStyleName("pin-backgroundcolor");
		}
		*/
		FlowPanel messagePanel = new FlowPanel();
		FlexTable fTable = new FlexTable();
		String name = poster.getFName();
		Label posterLabel = new Label(name+": ");
		posterLabel.addStyleDependentName("nameColor");
		fTable.setWidget(0, 0, posterLabel);
		fTable.setText(0, 1, this.content);
		messagePanel.add(fTable);

		if (!ClassConversationUtil.IsNullOrEmpty(videoURL) || !ClassConversationUtil.IsNullOrEmpty(imageURL)){
			FlexTable mediaTable = getMediaTable();
			messagePanel.add(mediaTable);
		}

		FlowPanel metaDataPanel = new FlowPanel();
		//metaDataPanel.addStyleName("relative");
		FlowPanel  leftDatePanel= new FlowPanel();
		String dateTime = DateTimeFormat.getFormat("yyyy-MM-dd h:mm a").format(postDate);
		Label dateLabel = new Label(dateTime);
		//dateLabel.addStyleDependentName("leftAlign");
		leftDatePanel.add(dateLabel);

		FlowPanel rightOptionPanel = new FlowPanel();
		rightOptionPanel.addStyleName("floatRight");
		final Label topic;
		if (!isTopic){
			topic = new Label("Make it a Topic");
		}
		else{
			topic = new Label ("Discuss It in Forum");
			topic.addClickHandler(new ClickHandler(){
				
				public void onClick(ClickEvent event) {
					Window.alert("Going to the forum to discuss it!");
				}

			});
		}
		final Label likeLabel = new Label("Like("+this.like+")");
		likeLabel.addClickHandler(new ClickHandler(){
			
			public void onClick(ClickEvent event) {
				likeLabel.setText("Like("+ ++like +")");
				updateLikeCounts();
			}

		});

		Label bar = new Label("|");
		FlexTable mTable = new FlexTable();

		topic.addStyleDependentName("linkleft");
		likeLabel.addStyleDependentName("linkleft");
		bar.addStyleDependentName("bar");
		rightOptionPanel.add(topic);
		rightOptionPanel.add(bar);
		topic.removeFromParent();
		bar.removeFromParent();
		rightOptionPanel.add(likeLabel);

		mTable.setWidget(0, 0, leftDatePanel);
		mTable.setWidget(0, 1, rightOptionPanel);
		
		mTable.setWidth(width +"px");
		metaDataPanel.add(mTable);
		fPanel.add(messagePanel);
		fPanel.add(metaDataPanel);
		
		FlexTable t = new FlexTable();
		final CheckBox ck = new CheckBox();
		
		t.setWidget(0, 0, ck);
		t.setWidget(0, 1, fPanel);
		ck.addClickHandler(new ClickHandler(){

			public void onClick(ClickEvent event) {
				if (ck.getValue()){
					//Window.alert("Checked");
					setChecked(true);
				}
				else{
					setChecked(false);
					//isChecked = false;
				}
				
			}
			
		});
		holdingPanel.add(t);
		
		return holdingPanel;
	}
	
	public void setChecked(boolean checked){
		this.isChecked = checked;
	}

	protected void updateLikeCounts() {
		ClassCommonsServiceAsync classCommonsServiceSvc = GWT.create(ClassCommonsService.class);
		// Set up the callback object.
		AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}
			
			public void onSuccess(Integer updated) {
				if (updated == 0){
					
				}
			}
		};
		classCommonsServiceSvc.updateLikeCount(this.mid, callback);


	}

	private FlexTable getMediaTable(){
		FlexTable fTable = new FlexTable();
		int column = 0;
		if (!ClassConversationUtil.IsNullOrEmpty(imageURL)){
			Image thumbImage = new Image(imageURL);
			thumbImage.addStyleDependentName("imageFormat");
			fTable.setWidget(0, column, thumbImage);
			final PopupPanel imagePopup = new PopupPanel(true);
			imagePopup.setAnimationEnabled(true);
			Image fullImage = new Image(imageURL);
			imagePopup.setWidget(fullImage);

			thumbImage.addClickHandler(new ClickHandler(){
				
				public void onClick(ClickEvent event) {
					imagePopup.center();			      
				}

			});
			column ++;
		}
		if (!ClassConversationUtil.IsNullOrEmpty(videoURL)){
			String vid = getVideoID();
			System.out.println("Video ID "+vid);
			Frame frame = new Frame("http://www.youtube.com/embed/"+vid);
			
			fTable.setWidget(0, column, frame);
		}

		return fTable;
	}
	
	public  String getVideoID(){
		String videoID = "";
		if (videoURL.contains("youtube.com")){
			int startIndex = videoURL.indexOf("v=")+2;
			if (startIndex != -1){
				String urlFocus = videoURL.substring(startIndex);
				int endIndex = urlFocus.indexOf("&");
				if (endIndex != -1){
					videoID = urlFocus.substring(0,endIndex);
				}
				else{
					videoID = urlFocus;
				}
			}
		
		}
		else if (videoURL.contains("youtu.be")){
			int startIndex = videoURL.lastIndexOf("/")+1;
			videoID = videoURL.substring(startIndex);
			
		}
		return videoID;
	}

	
    public int calculateXposition(int mouse_x, int window_width, int object_width){
        if (mouse_x + object_width > window_width) {
            return mouse_x - object_width;
        }
        else {
            return mouse_x;
        }
    }
    
    public int calculateYposition(int mouse_y, int window_heigth, int object_heigth){
        if (mouse_y + object_heigth > window_heigth) {
            return mouse_y - object_heigth;
        }
        else {
            return mouse_y;
        }
    }
    
    protected void prepareDropdownList(String cls_id, ListBox list) {
        final ListBox forum_list = list;
        String url = "http://cscl.ist.psu.edu:5678/classconversation/service/ForumService";
        String postData = "op=gforums&t=0&o=time asc&cls=" + cls_id;
        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
        builder.setHeader("Content-type", "application/x-www-form-urlencoded");
        try {
          Request request = builder.sendRequest(postData, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    forum_list.clear();
                    forum_list.addItem("Select...", "unselected");
                    JsArray<CommonObject> forums = asArrayOfCommonObject(response.getText());
                    CommonObject forum;
                    for (int i = 0; i < forums.length(); i++) { 
                        forum = forums.get(i);
                        forum_list.addItem(forum.getContent(), forum.getId());
                    }
                } else {
                }                
            }
          });
        } catch (RequestException e) {
        }
    }
    
    public native String[] getFiles(Element upload) /*-{
        var input = upload;
        var names = new Array();       
               for (var i = 0; i < input.files.length; i++) {
               names[i] = input.files[i].name;
    //            $wnd.alert(input.files[i].name);
               }
        return names;
    }-*/;
    
    private final native JsArray<CommonObject> asArrayOfCommonObject(String json) /*-{
        return eval(json);
    }-*/;
    
    public boolean isChecked(){
    	return this.isChecked;
    }

}
